package org.kinglone.back.friendlinks.action;

import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.kinglone.back.friendlinks.model.Friendlinks;
import org.kinglone.back.friendlinks.service.FriendlinksService;
import org.kinglone.common.util.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 友情链接控制层
 * @author yejinlong
 * 2017年8月2日
 * @version 1.0
 */
@Controller
@RequestMapping("/friendlinks")
public class FriendlinksAction {

	@Resource(name="friendlinksService")
	private FriendlinksService friendlinksService;

		//跳转到友情链接页面
		@RequestMapping("/friendlinks.jspx")
		public ModelAndView friendlinks(HttpServletRequest request){
			ModelAndView modelAndView = new ModelAndView("/WEB-INF/jsp/back/friendlinks/friendlinks.jsp");
			return modelAndView;
		}

		//跳转到添加友情页面
		@RequestMapping("/showAddLinks.jspx")
		public ModelAndView showAddLinks(HttpServletRequest request){
			ModelAndView modelAndView = new ModelAndView("/WEB-INF/jsp/back/friendlinks/addFriendLinks.jsp");
			return modelAndView;
		}

		//跳转到修改友情链接
		@RequestMapping("/showUpdatelinks.jspx")
		public ModelAndView showUpdatelinks(HttpServletRequest request){
			ModelAndView modelAndView = new ModelAndView("/WEB-INF/jsp/back/friendlinks/updateFriendLinks.jsp");
			String linksId = request.getParameter("linksId");
			Friendlinks friendlinks = friendlinksService.get(linksId);
			modelAndView.addObject("friendlinks",friendlinks);
			return modelAndView;
		}
	
		//添加友情链接
		@RequestMapping("/submitAddLinks.jspx")
		public JSONMessageView submitAddLinks(HttpServletRequest request){
			JSONMessageView json = new JSONMessageView(-1, "添加失败!", null);
			try {
				String linksId = request.getParameter("linksId");
				String nickname = request.getParameter("nickname");
				String weburl = request.getParameter("weburl");
				String phone = request.getParameter("phone");
				String remark = request.getParameter("remark");
				if(StringUtil.isNullOrEmpty(nickname)){
					json.setCode(-1);
					json.setMessage("请输入昵称!");
					return json;
				}
				if(StringUtil.isNullOrEmpty(weburl)){
					json.setCode(-1);
					json.setMessage("请输入博客地址!");
					return json;
				}
				if(StringUtil.isNullOrEmpty(linksId)){
					Friendlinks f = new Friendlinks();
					f.setCreateTime(DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
					f.setNickname(nickname);
					f.setPhone(phone);
					f.setRemark(remark);
					f.setWeburl(weburl);
					f.setIsDisplay("Y");
					friendlinksService.save(f);
					json.setCode(0);
					json.setMessage("添加成功!");
					return json;
				}else{
					Friendlinks friendlinks = friendlinksService.get(linksId);
					friendlinks.setIsDisplay("Y");
					friendlinks.setNickname(nickname);
					friendlinks.setPhone(phone);
					friendlinks.setRemark(remark);
					friendlinks.setWeburl(weburl);
					friendlinksService.update(friendlinks);
					json.setCode(0);
					json.setMessage("修改成功!");
					return json;
				}

			} catch (Exception e) {
				e.printStackTrace();
				json.setCode(-1);
				json.setMessage("添加失败!");
				return json;
			}
		}
		
			//分页查询友情链接
			@RequestMapping("/selLinksList.jspx")
			public JSONMessageView selLinksList(HttpServletRequest request){
				JSONMessageView json = new JSONMessageView(-1, "查询失败!", null);
				try {
					String nickname = request.getParameter("nickname");
					String phone = request.getParameter("phone");
					String pageNo = request.getParameter("pageNo");
					String pageSize = request.getParameter("pageSize");
					PageQuery pageQuery = new PageQuery();
					pageQuery.setStart((Integer.parseInt(pageNo)-1)*Integer.parseInt(pageSize));
					pageQuery.setLength(Integer.parseInt(pageSize));
					PageResults<Friendlinks> list = friendlinksService.findLinksList(pageQuery,nickname,phone);
					JSONArray totalJsonArray = new JSONArray();
					JSONObject totalJsonObject = new JSONObject();
					JSONArray jsonArray=new JSONArray();
					for(Friendlinks f : list.getResults()) {
						JSONObject jsonObject = new JSONObject();
						jsonObject.put("sysid", f.getSysid());
						jsonObject.put("nickname", f.getNickname());
						jsonObject.put("weburl", f.getWeburl());
						jsonObject.put("phone",f.getPhone());
						jsonObject.put("remark", f.getRemark());
						jsonObject.put("isDisplay", f.getIsDisplay());
						jsonArray.add(jsonObject);
					}
					totalJsonObject.put("pagetotal", list.getPageCount());//总页数
					totalJsonObject.put("info", jsonArray);
					totalJsonArray.add(totalJsonObject);
					json.setCode(0);
					json.setMessage("查询成功!");
					json.setContent(totalJsonArray);
					return json;
				} catch (Exception e) {
					e.printStackTrace();
					json.setCode(-1);
					json.setMessage("查询失败!");
					return json;
				}

			}
		
		//删除链接
		@RequestMapping("/dellinks.jspx")
		public JSONMessageView dellinks(HttpServletRequest request){
				JSONMessageView json = new JSONMessageView(-1,"删除失败!",null);
				try {
					String linksId = request.getParameter("linksId");
					if(StringUtil.isNullOrEmpty(linksId)) {
						json.setMessage("未取得参数");
						return json;
					}
					friendlinksService.deleteById(linksId);
					json.setCode(0);
					json.setMessage("删除成功!");
					return json;
				}catch (Exception e){
					e.printStackTrace();
					json.setCode(-1);
					json.setMessage("系统错误!");
					return json;
				}
		}


}
